package com.qf;

import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;

/**
 * 作者：铁蛋
 * 时间：2021/10/20 16:38
 * 描述：永无Bug
 */
public class MQProducer {
//    public static void main(String[] args) throws Exception {
////        发送消息的步骤
////        1.创建消息生产者producer(DefaultMQProducer)，并制定生产者group
//        DefaultMQProducer producer=new DefaultMQProducer("testgroup1");
////        2.指定Nameserver地址
//        producer.setNamesrvAddr("localhost:9876");
////        3.启动producer
//        producer.start();
////        4.创建Message对象，指定主题Topic、消息体
//        Message message=new Message("testtopic1","testtag1","hello ,mq".getBytes());
////        5.发送消息
//        SendResult result = producer.send(message);
//        System.out.println(result);
////        6.关闭生产者producer
//        producer.shutdown();
//    }

    public static void main(String[] args) throws Exception {
//        发送消息的步骤
//        1.创建消息生产者producer(DefaultMQProducer)，并制定生产者group
        DefaultMQProducer producer=new DefaultMQProducer("testgroup1");
//        2.指定Nameserver地址
        producer.setNamesrvAddr("localhost:9876");
//        3.启动producer
        producer.start();
//        4.创建Message对象，指定主题Topic、消息体
        Message message=new Message("testtopic1","testtag1","hello ,mq异步".getBytes());
        message.setKeys("testKey");
//        5.发送消息
            producer.send(message, new SendCallback() {
                @Override
                public void onSuccess(SendResult sendResult) {
                    //消息发送成功的回调
                    System.out.println(sendResult);
                    producer.shutdown();
                }

                @Override
                public void onException(Throwable throwable) {
                    //消息发送失败的回调
                    throwable.printStackTrace();
                    producer.shutdown();
                }
            });
    }
//    public static void main(String[] args) throws Exception {
////        发送消息的步骤
////        1.创建消息生产者producer(DefaultMQProducer)，并制定生产者group
//        DefaultMQProducer producer=new DefaultMQProducer("testgroup1");
////        2.指定Nameserver地址
//        producer.setNamesrvAddr("localhost:9876");
////        3.启动producer
//        producer.start();
////        4.创建Message对象，指定主题Topic、消息体
//        Message message=new Message("testtopic1","testtag1","hello ,mq单向".getBytes());
////        5.发送消息
//        producer.sendOneway(message);
//        producer.shutdown();
//    }
}
